package gui.utils;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.text.BadLocationException;
import javax.swing.text.Keymap;

public class MyAutoCompletion extends JPanel implements KeyListener,MouseListener{


	private JList list;
	private ArrayList<String> data;
	private JDialog dialog;
	private JScrollPane scroll;
	private JTextField field;
	public MyAutoCompletion(ArrayList<String> data,JFrame f){

		super();
		this.data = data;
		list = new JList(data.toArray());
		field = new JTextField();

		
		

		dialog = new JDialog(f);
		dialog.setUndecorated(true);
		
		scroll = new JScrollPane(list);
		
		
		
		
		dialog.getContentPane().add(scroll);
		dialog.setAlwaysOnTop(true);
		
		
		field.addKeyListener(this);
		field.addMouseListener(this);
		list.addMouseListener(this);
		

		
		this.setLayout(new BorderLayout());
		this.add(field,BorderLayout.CENTER);
	}

	public JDialog getDialog(){return dialog;}
	public static void main( String[] args ) {
		ArrayList<String> a = new ArrayList<String>();
		a.add("un");
		a.add("deux");a.add("quatre");
		a.add("troisfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
		a.add("trois");
		JFrame f = new JFrame();
		MyAutoCompletion my = new MyAutoCompletion(a,f);

		f.setSize(200,25);
		f.getContentPane().add(my);
		f.setVisible(true);

	}

	public void setColor(Color c){
		list.setBackground(c);
		this.setBackground(c);

	}

	public void changeData(Vector<String> a){
	
		data = new ArrayList<String>();
		data.addAll(a);
		list.setListData(getCompletion(field.getText().substring(0,field.getCaretPosition())).toArray());
	}

	public String getSelectedItem(){

		return field.getText();
	}

	public ArrayList<String> getCompletion(String s){
		ArrayList<String> compl = new ArrayList<String>();

		for (String d : data){

			if(d.contains(s)){
				compl.add(d);
			}
		}
		return compl;

	}

	
	public void keyPressed(KeyEvent e) {
		if (e.getSource() == list)
		{

			field.setText((String) list.getSelectedValue());

		}
	}

	public void showDialog(){
		if (!dialog.isVisible()){

			ArrayList<String> l = getCompletion(field.getText().substring(0,field.getCaretPosition()));
			list.setListData(l.toArray());
			list.setSelectedIndex(0);

			dialog.setFocusableWindowState(false);
			

			dialog.setSize(getWidth(), 200);
			
			
			dialog.setLocation((int)getLocationOnScreen().getX(),(int)getLocationOnScreen().getY()+getHeight());
			dialog.setVisible(true);
			list.setSelectedIndex(0);
		}
	}
	
	public void closeDialog(){
		dialog.setVisible(false);
	}
	
	
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

		if (dialog.isVisible()){
		
			
			if (e.getKeyCode() == 10){
				dialog.setVisible(false);
				field.setText((String) list.getSelectedValue());
				
				
			}
			else if (e.getKeyCode() == 38){
				list.setSelectedIndex(list.getSelectedIndex()-1);
				field.setText((String) list.getSelectedValue());
			}
			else if (e.getKeyCode() == 40){
				list.setSelectedIndex(list.getSelectedIndex()+1);
				field.setText((String) list.getSelectedValue());
			}
			else{
				ArrayList<String> l = getCompletion(field.getText().substring(0,field.getCaretPosition()));
				list.setListData(l.toArray());
				list.setSelectedIndex(0);
			}
			
			
		}
		else showDialog();
		
		
	}

	
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	public void paint(Graphics g){
		
	
		super.paint(g);
		dialog.setSize(getWidth(),200);
		dialog.setLocation((int)getLocationOnScreen().getX(),(int)getLocationOnScreen().getY()+getHeight());
		
		
	}

	
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
	
		if (e.getSource() == list){
			if (e.getClickCount() == 2){
				if (dialog.isVisible()){
					dialog.setVisible(false);
					field.setText((String) list.getSelectedValue());
				}
			}
		}
		if (e.getSource() == field){
			showDialog();
		}
		
	}

	
	public void mouseEntered(MouseEvent e) {
		
		
	}

	
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		/*if (e.getSource() == list){
			closeDialog();
			
		}*/
		
	}

	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	





}
